/**
 * 获取权限列表
 * @url admin/system/permission/sys/getList
 * @description 获取权限列表，支持分页和搜索
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let {
			pageIndex = 1,
			pageSize = 20,
			keyword = ''
		} = data;
		
		// 构建查询条件
		let where = {};
		if (keyword) {
			where = _.or([
				{ permission_id: new RegExp(keyword, 'i') },
				{ permission_name: new RegExp(keyword, 'i') }
			]);
		}
		
		// 查询权限列表
		let permissionRes = await db.collection('uni-id-permissions')
			.where(where)
			.skip((pageIndex - 1) * pageSize)
			.limit(pageSize)
			.orderBy('create_date', 'desc')
			.get();
		
		// 获取总数
		let countRes = await db.collection('uni-id-permissions').where(where).count();
		
		res.data = {
			list: permissionRes.data,
			total: countRes.total,
			pageIndex,
			pageSize
		};
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

